import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_train.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/common/common_dialog.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

import '../train_details_page.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/14.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class TrainApproveDetaisPage extends StatefulWidget {
  final TrainApproveBean approveBean;

  const TrainApproveDetaisPage({Key key, @required this.approveBean})
      : super(key: key);

  @override
  _TrainApproveDetaisPageState createState() => _TrainApproveDetaisPageState();
}

class _TrainApproveDetaisPageState extends State<TrainApproveDetaisPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("审批详情"),
      ),
      ///填充布局
      body: Container(
          width: double.infinity,
          height: double.infinity,
          child: Stack(
            alignment: Alignment.center,
            children: [
              Positioned.fill(
                top: 38,
                child: TrainDetailsPage(
                  approveBean: widget.approveBean,
                  isScaffold: false,
                ),
              ),
              Positioned(
                top: 12,
                left: 12,
                child: buildApproveStatues(),
              ),
              Positioned(
                bottom: 40,
                child: buildApproveButton(),
              )
            ],
          )),
    );
  }

  //按钮
  buildApproveButton() {
    if (widget.approveBean.approeStatues != 0) {
      return Container();
    }
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        buildCustomButton(Colors.blueAccent, "同意", () {
          showCheckAlert(1, "操作成功", "请确认 同意本次审批吗???");
        }),
        SizedBox(
          width: 16,
        ),
        buildCustomButton(Colors.redAccent, "拒绝", () {
          showCheckAlert(2, "操作成功", "请确认 真的要拒绝本次审批吗???");
        }),
      ],
    );
  }

  buildCustomButton(Color textColor, String statuesStr, Function() onTap) {
    return InkWell(
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.only(left: 10, right: 10, top: 2, bottom: 2),
        decoration: BoxDecoration(
            color: textColor,
            border: Border.all(color: textColor),
            borderRadius: BorderRadius.all(Radius.circular(4))),
        child: Text(
          statuesStr,
          style: TextStyle(color: Colors.white),
        ),
      ),
    );
  }

  //状态
  buildApproveStatues() {
    int approeStatues = widget.approveBean.approeStatues;
    String statuesStr = "";
    Color textColor = Colors.blueGrey;
    if (approeStatues == 0) {
      statuesStr = "等待审核中";
    } else if (approeStatues == 1) {
      statuesStr = "已通过";
      textColor = Colors.green;
    } else if (approeStatues == 2) {
      statuesStr = "已拒绝";
      textColor = Colors.red;
    } else if (approeStatues == 3) {
      statuesStr = "已撤消";
      textColor = Colors.orange;
    } else if (approeStatues == 4) {
      statuesStr = "待处理";
      textColor = Colors.red;
    }
    return Container(
      padding: EdgeInsets.only(left: 10, right: 10, top: 2, bottom: 2),
      decoration: BoxDecoration(
          border: Border.all(color: textColor),
          borderRadius: BorderRadius.all(Radius.circular(4))),
      child: Text(
        statuesStr,
        style: TextStyle(color: textColor),
      ),
    );
  }

  showCheckAlert(int statues, String message, String contentMessage) {
    showCommonAlertDialog(
        context: context,
        selectText: "确定",
        cancleText: "再考虑一下",
        contentMessag: contentMessage,
        selectCallBack: () {
          alertTrainApproDetailsFunction(statues, message);
        });
  }

  Future<bool> alertTrainApproDetailsFunction(
      int statues, String message) async {
    Map<String, dynamic> monthMap = {
      "id": widget.approveBean.id,
      "statues": statues,
    };
    //答题数据
    ResponseInfo statisRresponseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.trainApproveUpdate,
      queryParameters: monthMap,
    );
    if (statisRresponseInfo.success) {
      ToastUtils.showToast(message);
    } else {
      ToastUtils.showToast("操作失败");
    }
    Navigator.of(context).pop();
    return Future.value(true);
  }
}
